/*!
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 * 
 * 消息推送实现
 * @author ThinkGem
 * @version 2015-5-29
 */

// 消息监听
$(document).ready(function() {
	dwr.engine.setActiveReverseAjax(true);
	dwr.engine.setNotifyServerOnPageUnload(true);
	dwr.engine.setErrorHandler(function(e){log(e)});
	MsgPushService.loginIndex(function(data) {
		var msgList = $("#msgList").empty();
		var num = data.count, list = data.list, html = "";
		// 添加到消息列表
		for (i = 0; i < list.length; i++) {
			var id = list[i].idKey, time = dateToStr(list[i].message.sendDate);
			msgList.append(templateRender($('#msgListTpl').html(), {id: id, time: time, message: list[i].message}));
		}
		if(num == 0){
			$("#msgNumber").empty().append(0);
			$("#msgTitle").empty().append("暂无最新消息");
		}else{
			$("#msgNumber").empty().append(num);
			$("#msgTitle").empty().append("您还有 " + num + " 项未读消息");
		}
		$(".timeago").timeago();
	});
});

// 接收消息 
function receiveMsg(message, recordId) {
	var id = recordId, time = dateToStr(message.sendDate), buttonList = JSON.parse(message.buttons);
	var msg = templateRender($('#msgTipTpl').html(), {id: id, time: time, message: message, buttonList: buttonList})
	showMessage(msg, '新消息提醒', 'info', 1000*60);
	// 添加到消息列表
	var num = parseInt($("#msgNumber").text());
	if(num !== "" || num !== NaN){
		if(num >= 10){
			$("#msgList li:last").remove()
		};
		$("#msgList").prepend(templateRender($('#msgListTpl').html(), {id: id, time: time, message: message}));
		$("#msgNumber").empty().append(++num);
		$("#msgTitle").empty().append("您还有 " + num + " 项未读消息");
		MsgPushService.deliveredMsg(recordId);
		$(".timeago").timeago();
	}
	doFlashTitle(); // 新消息标题闪动
}

// 读取消息
function readMsg($this, title, recordId) {
	addTabPage($($this), title, $($this).data("href") , null, true, false);
	MsgPushService.readMsg(recordId);
	var msgRecord = $("#msg" + recordId);
	if (msgRecord.length > 0){
		var num = parseInt($("#msgNumber").text());
		$("#msgNumber").empty().append(--num);
		$("#msgTitle").empty().append("您还有 " + num + " 项未读消息");
		msgRecord.remove();
	}
}

// 标题新消息闪动提醒，调用doFlashTitle()即可
var isWindowFocus = true; // 当前浏览器窗口是否处于焦点
if ("onfocusin" in document) {// for IE 
	document.onfocusin = function (){isWindowFocus = true};
	document.onfocusout = function (){isWindowFocus = false};
} else {
	window.onfocus = function (){isWindowFocus = true};
	window.onblur = function (){isWindowFocus = false};
}
var flashStep = 0; // 交替变量
var flashTitleRun = false; // 是否正在执行
var normalTitle = document.title; // 保存原标题
var flashTitle = function() {
	//仅窗口不在焦点时闪烁title，回到焦点时停止闪烁并将title恢复正常
	if (isWindowFocus) {// 当前处于焦点
		document.title = normalTitle;
		flashTitleRun = false;
		return;// 退出循环
	}
	flashTitleRun = true; flashStep++;
	if (flashStep == 3) { flashStep = 1; }
	if (flashStep == 1) { document.title = "【新消息】" + normalTitle; }
	if (flashStep == 2) { document.title = "【　　　】" + normalTitle; }
	setTimeout("flashTitle()", 500); //循环
}
//调用这个执行标题闪烁，而不是直接调用flashTitle，保证多次调用只会执行一次。
function doFlashTitle() {
	if (!flashTitleRun){//没有执行时，才执行
		flashTitle();
	}
}